using System;
using System.Collections.Generic;
using System.Linq;
using ApiProject.Api.Data;
using ApiProject.Api.Entity;
using ApiProject.Api.PramaModel;
using ApiProject.Api.Repository;
using ApiProject.Api.Uitls;
using ApiProject.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;

//后台用户登录
namespace ApiProject.Api.Controllers
{

    //管理员登录并获取token
    [ApiController]
    [Route("[controller]")]
    public class PictureController : ControllerBase
    {



        private readonly IConfiguration _configuration;

        private readonly IWebHostEnvironment _hostingEnvironment;


        public PictureController(
            IConfiguration configuration,
IWebHostEnvironment hostingEnvironment


        )
        {

            _configuration = configuration;
            _hostingEnvironment = hostingEnvironment;
        }
        // 添加图片
        //Post /picture
        [HttpPost]
        public dynamic AddPicture()
        {
            var files = HttpContext.Request.Form.Files;

            string webRootPath = _hostingEnvironment.WebRootPath; // ...Api/wwwroot
            var showfilePath = PicHelper.getFilePath(files, "upload", webRootPath);

            Console.WriteLine(showfilePath);

            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = showfilePath,
                Msg = "添加图片成功"
            });
        }


        //删除文章图片
        //Post /article
        [HttpPut]
        public dynamic Put(NewPicture model)
        {
            var str = model.PicPath;

            string webRootPath = _hostingEnvironment.WebRootPath; // ...Api/wwwroot

            var picpath = webRootPath + str;

            FileHelper.FileDel(picpath);

            return "Ok";


        }
    }
}




